import { GetScoreExchange, SetScoreExchange } from "~/api";

export const useContent = () => {
    const dialogTemplateChooseRef = ref(null);
    const dialogStore = useDialogStore();
    const exchangeList = ref([]);
    let exchangeCode = null;

    const onTaskList = () => {
        dialogStore.isTaskListDialogVisible = true;
    };

    const onScoreLottery = () => {
        dialogStore.isScoreLotteryDialogVisible = true;
    };

    const setScoreExchange = async (code, id) => {
        const res = await SetScoreExchange({
            code,
            id
        });

        if (res.code == 200) {
            GET_MIDNUM();
            GET_SCORE_LOG();
            Message().success("兑换成功");
        } else {
            Message().warning(res.msg);
        }
    }

    const onExchange = async (item) => {
        if (item.type == 'vip') {
            exchangeCode = item.code;
            dialogTemplateChooseRef.value.open(undefined, true);
        } else {
            exchangeCode = null;
            setScoreExchange(item.code);
        }
    };

    const getScoreExchange = async () => {
        const res = await GetScoreExchange();
        exchangeList.value = res.data;
    };

    const onConfirm = async (id) => {
        setScoreExchange(exchangeCode, id);
    }

    return {
        dialogTemplateChooseRef,
        exchangeList,

        getScoreExchange,
        onTaskList,
        onScoreLottery,
        onExchange,
        onConfirm
    };
};
